package com.hgk.common.common.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.common.android.volley.VolleyError;
import com.common.android.volley.toolbox.RequestListener;
import com.common.android.volley.toolbox.VolleyManager;
import com.common.dongqiuhui.apps.comm.model.BaseController;
import com.common.handmark.pulltorefresh.library.PullToRefreshBase;
import com.common.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.common.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.common.handmark.pulltorefresh.library.PullToRefreshLocationListView;
import com.common.handmark.pulltorefresh.library.internal.BallBaseAdapter;
import com.hgk.common.common.ui.view.RippleView;
import com.hgk.common.common.ui.view.TipView;

import java.util.List;
import java.util.Map;

import width.hgk.wanwan.R;

/**
 * 上拉下拉刷新框架
 *
 * @author fyh
 *
 */
public abstract class UpAndDownLocationRefreshActivity extends BallTitleAcitivty
        implements OnRefreshListener2<ListView>, OnItemClickListener {

    public PullToRefreshLocationListView mPullRefreshListView;

    private BallBaseAdapter mBaseAdapter;
    /**
     * 是否第一次加载
     */
    protected boolean mIsFirstLoading = true;
    protected TipView mTipView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPullRefreshListView = (PullToRefreshLocationListView) findViewById(R.id.pull_location_refresh_list);
        mPullRefreshListView.setShowIndicator(false);
        mPullRefreshListView.setMode(onCreate());
        mTipView=new TipView(mContext);
        mPullRefreshListView.setEmptyView(mTipView);
        initListViewStyle();
    }
    private void initListViewStyle(){
        mPullRefreshListView.getRefreshableView().setSelector(mContext.getResources().getDrawable(R.drawable.transparent));
        mPullRefreshListView.setShowIndicator(false);
        setDivider(R.color.dividercolor);
        mPullRefreshListView.setMode(onCreate());
        initAdapter(getAdapte());
    }

    /**
     * 加载数据
     */
    protected void getNetDataForPost(final String action,String url,Class classOfT){
        mTipView.startAnim();
        VolleyManager.getInstance().postString(url, getApiParams(action), new RequestListener() {
            @Override
            public void requestSuccess(Object result) {
                mTipView.stopAnim();
                if (mTipView != null) {
                    mPullRefreshListView.removeOldEmptyView(mTipView);
                }
                BaseController baseController;
                mPullRefreshListView.setLoadMore();
                if (result instanceof BaseController) {
                    baseController = (BaseController) result;
                    if ("10000".equals(baseController.getCode())) {
                        success(action, result);
                    } else {
                        fail(action, baseController.getMsg());
                    }
                    mIsFirstLoading = false;
                }

            }

            @Override
            public void requestError(VolleyError e) {
                mIsFirstLoading = false;
                if (e.isNonetResponse()) {
                    if (mIsFirstLoading) {
                        if (mBaseAdapter.getCount()<=0){
                            noNetView();
                        }else{
                            Toast.makeText(mContext, getResources().getString(R.string.nonet),
                                    Toast.LENGTH_SHORT).show();
                            CompletLoading();
                        }
                    } else {
                        Toast.makeText(mContext, getResources().getString(R.string.nonet),
                                Toast.LENGTH_SHORT).show();
                        mPullRefreshListView.setLoadMore();
                        CompletLoading();
                    }
                }

            }
        }, classOfT);
    }


    protected void getNetDateForGet(final String action,String url,Class classInfo){
        VolleyManager.getInstance().get(url, getApiParams(action), new RequestListener() {
            @Override
            public void requestSuccess(Object result) {
                BaseController baseController;
                mPullRefreshListView.setLoadMore();
                if (result instanceof BaseController) {
                    baseController = (BaseController) result;
                    if ("10000".equals(baseController.getCode())) {
                        success(action, result);
                    } else {
                        fail(action, baseController.getMsg());
                    }
                    mIsFirstLoading = false;
                }
            }

            @Override
            public void requestError(VolleyError e) {
                if (e.isNonetResponse()) {
                    if (mIsFirstLoading) {
                        noNetView();
                    } else {
                        Toast.makeText(mContext, getResources().getString(R.string.nonet),
                                Toast.LENGTH_SHORT).show();
                    }
                }
                mIsFirstLoading = false;
                mPullRefreshListView.setLoadMore();
                fail(action, e.getMessage());
            }
        }, classInfo);
    }
    /**
     * 初始化 adapter
     *
     */
    protected void initAdapter(BallBaseAdapter adapter) {
        this.mBaseAdapter = adapter;
        mPullRefreshListView.setAdapter(mBaseAdapter);
        mPullRefreshListView.setOnRefreshListener(this);
        mPullRefreshListView.setOnItemClickListener(this);
    }

    /**
     * 设置item颜色
     *
     * @param imgId
     */
    protected final void setDivider(int imgId) {
        if (imgId == 0) {
            mPullRefreshListView.getRefreshableView().setDivider(null);
        } else {
            mPullRefreshListView.getRefreshableView().setDivider(
                    this.getResources().getDrawable(imgId));
        }
    }

    /**
     * 设置item之间的DividerHeight
     *
     * @param height
     */
    protected final void setDividerHeight(int height) {
        mPullRefreshListView.getRefreshableView().setDividerHeight(height);
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        if (!mIsFirstLoading) {
            onDownRefresh();
        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        if (!mIsFirstLoading) {
            onUpRefresh();
        }
    }

    /**
     * 设置适配器
     *
     * @return
     */
    protected abstract BallBaseAdapter getAdapte();

    /**
     * 初始化
     *
     */
    protected abstract Mode onCreate();

    /**
     * 下拉刷新事件
     */
    protected abstract void onDownRefresh();

    /**
     * 上拉刷新时间
     */
    protected abstract void onUpRefresh();


    /**
     * Api参数
     */
    protected abstract Map getApiParams(String action);

    /**
     * 成功
     */
    protected  abstract void success(String action,Object result);

    protected abstract void fail(String action,String failStr);

    /**
     * 刷新数据
     */
    public void CompletLoading(){
        mBaseAdapter.notifyDataSetChanged();
        mPullRefreshListView.onRefreshComplete();
        mPullRefreshListView.setDataLoadingState(false);
    }

    /**
     * 设置list显示
     *
     * @param visibility
     */
    protected final void setListVisibility(int visibility) {
        mPullRefreshListView.getRefreshableView().setVisibility(visibility);
    }

    // mPullRefreshListView.setDataLoadingState(false);

    /**
     * 加载结束关闭刷新动画
     */
    protected  void onEndRefresh() {
        hideLoadingView();
        mPullRefreshListView.setDataLoadingState(false);
        mPullRefreshListView.onRefreshComplete();
        mBaseAdapter.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    protected final void clearData() {
        mBaseAdapter.clear();
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public  void setData(List data) {
        mBaseAdapter.setData(data);

    }
    public void setDateForEquals(List data){
        mBaseAdapter.setDataForEquals(data);
    }
    /**
     * 尾添加一条数据
     *
     * @param data
     */
    public void setDataLast(Object data) {
        mBaseAdapter.setDataLast(data);
    }
    public int getCount(){
        return mBaseAdapter.getCount();
    }
    /**
     * 头部添加一组数据
     *
     * @param datas
     */
    public void setDataToHader(List datas) {
        mBaseAdapter.setMultitermDataToHader(datas);
    }

    /**
     * 添加头
     *
     * @param v
     */
    public void addHead(View v) {
        mPullRefreshListView.getRefreshableView().addHeaderView(v);
    }

    /**
     * 删除指定 ID
     *
     * @param position
     */
    public void removeData(int position) {
        mBaseAdapter.removeData(position);
    }
    /**
     * 删除指定 ID
     *
     * @param position
     */
    public Object getData(int position) {
        return mBaseAdapter.getItem(position);
    }

    /**
     * 删除指定 t
     *
     * @param t
     */
    public void removeData(Object t) {
        mBaseAdapter.removeData(t);
    }

    /**
     * 设置listView显示在底部
     */
    public void setSelectionFoot() {
        try{
            mPullRefreshListView.getRefreshableView().setSelection(mBaseAdapter.getCount());
        }catch(Exception e){
            e.printStackTrace();
        }

        //mPullRefreshListView.getRefreshableView().setSelection(
        //	mBaseAdapter.getCount() - 1);
    }



    @Override
    public int getBodyView() {
        return R.layout.ball_list_location_view;
    }

    /**
     * 获取PullToRefreshListView
     *
     * @return
     */
    public PullToRefreshLocationListView getPullRefreshListView() {
        return mPullRefreshListView;
    }
    public void noNetView(){
        mTipView.getmIvLoading().setVisibility(View.GONE);
        mTipView.getmTvLoading().setVisibility(View.GONE);
        mTipView.stopAnim();
        mTipView.getmRvNoNet().setVisibility(View.VISIBLE);
        mTipView.getmIvNoNet().setVisibility(View.VISIBLE);
        mTipView.getmTvNoNet().setVisibility(View.VISIBLE);
        mTipView.getmIvNoNet().setBackgroundResource(R.drawable.nonet_icon);
        mTipView.getmRvNoNet().setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                noNetReflushBtn();
            }
        });
        mPullRefreshListView.setEmptyView(mTipView);
    }
    public void ShowNoDataView(String tipStr){
        mPullRefreshListView.setMode(Mode.DISABLED);
        mTipView.getmIvLoading().setVisibility(View.GONE);
        mTipView.getmTvLoading().setVisibility(View.GONE);
        mTipView.stopAnim();
        mTipView.getmIvNoNet().setBackgroundResource(R.drawable.nodata_icon);
        mTipView.getmRvNoNet().setVisibility(View.GONE);
        mTipView.getmIvNoNet().setVisibility(View.VISIBLE);
        mTipView.getmTvNoNet().setVisibility(View.VISIBLE);
        mTipView.getmTvNoNet().setText(tipStr);
        mPullRefreshListView.setEmptyView(mTipView);
    }
    public void showLoadingView(){

        mTipView.getmIvLoading().setVisibility(View.VISIBLE);
        mTipView.startAnim();
        mTipView.getmTvLoading().setText("");
        mTipView.getmRvNoNet().setVisibility(View.GONE);
        mTipView.getmIvNoNet().setVisibility(View.GONE);
        mTipView.getmTvNoNet().setVisibility(View.GONE);
    }
    // 无网络刷新按钮事件
    public void noNetReflushBtn() {

    }
}